<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
class CouponsViewCoupons extends JView
{
    protected $pagination;
    protected $items;
    protected $state;
    
    private $users=array();
    private $categories=array();
    private $shops=array();
    
    // Overwriting JView display method
    function display($tpl = null) 
    {
            $this->pagination = $this->get('Pagination');
            $this->items = $this->get('Items');
            $this->state = $this->get('State');
            
            JToolBarHelper::title(   JText::_( 'COM_COUPONS_COUPONS_MANAGER' ).": ".JText::_( 'COM_COUPONS_COUPONS' ), 'generic.png' );
            JToolBarHelper::addNewX('coupon.add');
            JToolBarHelper::editListX('coupon.edit');
            JToolBarHelper::divider();
            JToolBarHelper::deleteList('',"coupons.remove");

           
            if ($this->state->get('filter.state') != 2)
            {
                    JToolBarHelper::divider();
                    JToolBarHelper::publish('coupons.publish', 'JTOOLBAR_PUBLISH', true);
                    JToolBarHelper::unpublish('coupons.unpublish', 'JTOOLBAR_UNPUBLISH', true);
            }

            if ($this->state->get('filter.state') != -1)
            {
                    JToolBarHelper::divider();
                    if ($this->state->get('filter.state') != 2)
                            JToolBarHelper::archiveList('coupons.archive');
                    elseif ($this->state->get('filter.state') == 2)
                            JToolBarHelper::unarchiveList('coupons.publish');
            }

            // Check for errors.
            if (count($errors = $this->get('Errors'))) 
            {
                    JError::raiseError(500, implode('<br />', $errors));
                    return false;
            }

            parent::display($tpl);
    }

    public function getCategory($catid)
    {
        if(array_key_exists($catid, $this->categories))
                return $this->categories[$catid];
        $model = $this->getModel();
        $category = $model->getCategory($catid);
        $this->categories[$catid] = $category->title;
        return $category->title;
    }
    
    public function getUser($userId)
    {        
        if(array_key_exists($userId, $this->users))
                return $this->users[$userId];
        $user = JUser::getInstance($userId);
        $this->users[$userId] = $user->username;
        return $user->username;
    }
   
    public function getShop($id)
    {        
        if(array_key_exists($id, $this->shops))
                return $this->shops[$id];
        $model = $this->getModel();
        $shop = $model->getShop($id);
        $this->shops[$id] = $shop->name;
        return $shop->name;
    }
}
?>